﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 徐汉权
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillValues();
            }
            else 
            {
            
            }
        }
        public static string FillValues() 
        {
            var sql = string.Format("select Id,Title,Content,Author,IsActived,IsDeleted,CreatedTime,UpDatedTime,Remarks from ");
            Dbepr.dataTable(sql);
            return sql;
        }
        //
        public string GveGetValues(int x,int y) 
        {
            var con = Gv_One.Rows[x].Cells[y];
            var res = con.Controls.Count > 0 ? ((TextBox)con.Controls[0]).Text : con.Text;
            return res;
        }
        //取消编辑
        protected void Gv_One_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Gv_One.EditIndex = -1;
            FillValues();
        }
        //编辑
        protected void Gv_One_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Gv_One.EditIndex = e.NewEditIndex;
            FillValues();
        }
        //更新
        protected void Gv_One_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = GveGetValues(e.RowIndex, 0);
            var title = GveGetValues(e.RowIndex, 1);
            var content = GveGetValues(e.RowIndex, 2);
            var author = GveGetValues(e.RowIndex, 3);
            var IsActived = GveGetValues(e.RowIndex, 4);
            var IsDeleted = GveGetValues(e.RowIndex, 5);
            var CreatedTime = GveGetValues(e.RowIndex, 6);
            var UpdatedTime = GveGetValues(e.RowIndex, 7);
            var Remarks = GveGetValues(e.RowIndex, 8);
            var sud = string.Format("Update Articles set Title='{1}',Content='{2}',Author='{3}',IsActived='{4}',IsDeleted='{5}',CreatedTime='{6}',UpdatedTime='{7}',Remarks='{8}' where Id = '{0}'",title,content,author,IsActived,IsDeleted,CreatedTime,UpdatedTime,Remarks,id);
            Dbepr.OrUpdataOrDelete(sud);
            FillValues(); 
            }
                //删除
        protected void Gv_One_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GveGetValues(e.RowIndex, 1);
            var s = string.Format("Delete from Articles where Id = {0}",id);
            Dbepr.OrUpdataOrDelete(s);
            FillValues();
        }

        protected void Gv_One_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {

        }
    }
}